home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / (Notes) / CopyBits slavishly.doc < prev    next >
Text File  |  1994-11-02  |  18KB  |  289 lines

  1. Most users of the VideoToolbox, especially GDOpenWindow, will want to read this document. This document contains my (slightly abbreviated) correspondence with with Don Moccia at Apple Developer Services about how to get CopyBits to faithfully copy the pixels from one image to another. Don illuminated several obscure but important points about how color tables and palettes affect CopyBits. (Apple Developer Support only accepts questions from certified Apple Partners. APDA can give you application information. Basically you have to have produced a commercial product for the Mac, e.g. the ISR Video Attenuator, and pay $1,500/year.)
  2. Denis Pelli
  3. -------------------------------------------------------
  4. Subject:CopyBits                                      1:40 AM
  5. 10/20/1994
  6.  
  7. (I've just become an Apple Partner.)
  8.  
  9. THE PROBLEM:
  10. I want to use CopyBits addOver mode to quickly add two 8-bit GWorlds. That is,
  11. I want to add the index stored in each pixel of the first GWorld to the index
  12. in the corresponding pixel of the second GWorld. No matter what I try, CopyBits
  13. seems to add the colors, not the indexes.
  14.  
  15. Both GWorlds and the current device have the same gray color table. Both
  16. GWorlds have palettes based on that gray color table, with every entry marked
  17. pmExplicit. As suggested by Inside Mac VI, page 20-17, I set bit 14 of the
  18. ctFlags field of the color table. I tried lots of other variants as well, but I
  19. never managed to get simple addition of the indexs.
  20.  
  21. Is it possible to get CopyBits to just add the raw pixels?
  22.  
  23. Denis Pelli
  24. Professor of Neuroscience
  25. Syracuse University
  26.  
  27. BACKGROUND
  28. I have 8 Macs of various vintages (II to 6100/60). I publish on info-mac a
  29. VideoToolbox of C source code for accurate synthesis of visual images for
  30. vision research on Macs that is used by hundreds of my colleagues. I'm using
  31. System 7 Pro and Metrowerks CodeWarrior C.
  32.  
  33. --------------------------- 
  34. Denis,
  35.  
  36. Since this seems to be your first technical question as an Apple Partner, I
  37. wish I could give you a more helpful answer. As you've found out, the
  38. arithmetic transfer modes work in color space, not  index space. The reason is
  39. that the Macintosh color table is easily and frequently modified. Because of
  40. this color space based operations provide a more reliable and consistent
  41. approach to color management. This is documented in "Inside Macintosh Imaging
  42. with QuickDraw,", p.4-38.
  43.  
  44. >> As suggested by Inside Mac VI, page 20-17, I set bit 14 of the ctFlags field
  45. of the color table.
  46.  
  47. FYI - This technique is usually used when coordinating a GWorld with a window
  48. that has an attached palette.
  49.  
  50. If you want to give us some idea of what you are trying to accomplish, perhaps
  51. we can come up with an alternative approach. For example, if CopyBits speed is
  52. an issue, you might be able to use the techniques in the QuickDraw Tech Note
  53. "QD 21 - Time and Space and CopyBits" on any Reference Edition Developer CD,
  54. e.g. September `94.
  55.  
  56. Just send a link to my attention if you think I can be of any help.
  57.  
  58. Regards,
  59.  
  60.  
  61. Don Moccia
  62. Apple Developer Support
  63. Reference Number 117781
  64.  
  65. ----------------------------------------- 
  66. Dear Don
  67.  
  68. thanks for the quick reply. I'm disappointed to hear that the addOver mode won't work directly on the indexs, but I can live with that, as I've written my own code.
  69.  
  70. However, could you clarify for me what I have to do to get slavish copying of pixels, without translation, using srcCopy mode? I think I've read all the relevant manuals several times, but I never quite get it, in large part because the manuals discuss CopyBits in one place and the Palette manager in another, leaving it quite unclear what the implications of the Palettes are for the use of CopyBits. I want to copy between GWorlds, or from them to color windows. I'm doing fancy image processing on the GWorlds, using my own code. The images are always grayscale. I don't want any color translation, partly for reasons of speed (Yes, thanks, I have read "Of time space and copybits"--it was helpful), and partly because I have gone to a lot of trouble to calculate these images exactly right, and I don't want them messed with, just copied, quickly. Is it enough for the color tables (source and current device) to have the same ctSeed? 
  71.  
  72. (In our typical setup we have several monitors, and use one to show the processed images. This monitor is given a special gray color table. In fact, though it's not directly relevant to our discussion, I go behind QuickDraw's back and do driver level calls to set the CLUT to take full advantage of a custom harware scheme that combines the RGB outputs electrically to create a more-than-8-bit precision grayscale signal that drives the monitor. See Pelli, D. G. and Zhang, L. (1991) Accurate control of contrast on microcomputer displays. Vision Research, 31, 1337-1350. As a result, I really don't care what color table I officially load into the device, anything to make CopyBits happy. At present I'm using the default grayscale color table.)
  73.  
  74. I normally add a palette to all my GWorlds with all entries marked as explicit, so that I can use pmForeColor. I presume that this will affect CopyBits srcCopy, but it's not clear to me how. (Should I also be setting pmTolerant, in addition to pmExplicit?). It is utterly unclear to me whether adding a palette to the DESTINATION makes any difference. What exactly is the effect of setting bit 14 of the ctFlags field of the SOURCE pixmap's color table? (You said, "FYI - This technique is usually used when coordinating a GWorld with a window that has an attached palette." Could you spell out for me how this bit affects CopyBits? The manual is enigmatic.) What is the effect of setting bit 14 of the ctFlags field of the DESTINATION pixmap's color table? None? What is the effect of setting bit 14 of the ctFlags field of the CURRENT DEVICE's pixmap's color table? (As far as I know there is no way to associate a Palette with a device.)
  75.  
  76. As you see, I've got things working, but I'm unclear on what things affect CopyBits srcCopy, and how, so I'm not confident that they will continue working. Again, my question is, how do palettes affect CopyBits srcCopy? I'd be grateful for any light you can shed on this. 
  77.  
  78. regards
  79.  
  80. denis
  81.  
  82. Denis Pelli
  83. Syracuse University
  84.  
  85. p.s. Will the New Inside Mac "Advanced Color Imaging" explain how palettes affect CopyBits? Any idea when it will be available?
  86.  
  87. --------------------------------------
  88.  
  89. Hi Denis,
  90.  
  91. I apologize for the cut-and-paste style, but you've asked a lot of questions
  92. and I want to make sure I address them all. I've tried to group related
  93. questions together, I hope it's not too much of a mishmash.
  94.  
  95. >> ...what I have to do to get slavish copying of pixels, without translation,
  96. using srcCopy mode?
  97. >> Is it enough for the color tables (source and current device) to have the
  98. same ctSeed?
  99.  
  100. QuickDraw relies on ctSeed to tell it if color tables are identical or not. If
  101. the two color tables have the same ctSeed, QuickDraw assumes they are the same.
  102. Naturally, you can get odd results if the color tables in fact differ. To get
  103. your slavish copying you need to ensure that the color tables are identical,
  104. which brings us to the next section...
  105.  
  106. >> I normally add a palette to all my GWorlds with all entries marked as
  107. explicit, so that I can use pmForeColor.
  108.  
  109. From one of our Macintosh Palette Manager Q&As on the Reference Edition
  110. Developer CDs: "The Macintosh Palette Manager doesn't work on offscreen
  111. environments the way you'd expect. Unlike color windows, SetPalette will not
  112. change the offscreen's color table; rather it just allows you to use
  113. PmForeColor and PMBackColor to set the current drawing color in that
  114. environment. To change the offscreen's color table you'll need to convert the
  115. palette to a color table and then set the resulting color table to the off
  116. screen. Calling Palette2CTab will do the converting for you. To get around
  117. having to use palettes to define the current drawing color in the off screen,
  118. you can always use Index2Color and then RGBForeColor to get the color to be set
  119. for drawing."
  120.  
  121. So you'll need to convert your palette to a color table and then use that color
  122. table to create the GWorld. The QuickDraw Snippet "Out of This GWorld" on Tool
  123. Chest Edition Developer CDs, e.g. November `94, shows this technique in the
  124. createGWorld() routine in the out.c file.
  125.  
  126. >> (Should I also be setting pmTolerant, in addition to pmExplicit?).
  127.  
  128. Although it doesn't apply in this case, you need to set pmTolerant to zero if
  129. you want to guarantee that the precise color is present.
  130.  
  131. >> What exactly is the effect of setting bit 14 of the ctFlags field of the SOURCE pixmap's color table?
  132. >> Could you spell out for me how this bit affects CopyBits?
  133.  
  134. A clearer explanation of this is in the QuickDraw Tech Note "QD 13 - Principia
  135. Off-Screen Graphics Environments." If bit 14 of ctFlags is set, " then the
  136. value field of each ColorSpec record in the ctTable array is used by _CopyBits
  137. as an index into the color palette that's attached to the destination window,
  138. and the rgb field is ignored."
  139.  
  140. >> What is the effect of setting bit 14 of the ctFlags field of the DESTINATION pixmap's color table?
  141. >> What is the effect of setting bit 14 of the ctFlags field of the CURRENT DEVICE's pixmap's color table?
  142.  
  143. There are no effects documented and, as far as I know, no effects in real life
  144. either.
  145.  
  146. >> ...the manuals discuss CopyBits in one place and the Palette manager in another, leaving it quite unclear what the implications of the Palettes are for the use of CopyBits.
  147. >>  Will "Advanced Color Imaging" explain how palettes affect CopyBits? Any idea when it will be available?
  148.  
  149. "Advanced Color Imaging" is very late. Early versions of some of the chapters
  150. may appear on the December Developer CD. I won't guess about the actual
  151. publishing date.
  152.  
  153. I don't think the Palette Manager chapter has undergone much of a review yet,
  154. so it is probably pretty similar to Inside Mac VI. I definitely feel that the
  155. relationship of palettes to GWorlds and CopyBits needs clarification. I will be
  156. reviewing the chapter and will keep your comments in mind. If you read the
  157. chapter and want to make comments, please feel free to send them to me and I
  158. will give them to the editor.
  159.  
  160. Let me know if I left anything out. Also, your article sounds interesting. Do
  161. you have an electronic copy you can send?
  162.  
  163. Regards,
  164.  
  165. Don Moccia
  166. Apple Developer Support
  167. Reference Number 117781
  168. -----------------------------------------
  169.  
  170. Dear Don
  171.  
  172. many thanks. Your reply was thorough and very helpful. However, I'm still puzzled by bit 14 of ctFlags.
  173.  
  174. As you requested, I'm enclosing a draft of my paper. It's a WriteNow 4 document. I'd also like to mail you a reprint of the final published version. Could you give me your mailing address? (This is just for your interest. It's irrelevant to my question.)
  175.  
  176. Here's the bit that puzzled me. You said, 
  177. "A clearer explanation of this is in the QuickDraw Tech Note "QD 13 - Principia Off-Screen Graphics Environments." If bit 14 of ctFlags is set, " then the value field of each ColorSpec record in the ctTable array is used by _CopyBits as an index into the color palette that's attached to the destination window, and the rgb field is ignored."
  178.  
  179. That doesn't make sense to me. CopyBits only receives a destination PixMap, so how does it know which window is relevant in order to find "the color palette that's attached to the destination window"? Sounds like magic. QD13 goes on to say that this is "explained in IM VI-20-17". So I re-read that too. I'm still baffled. I don't understand what it's saying and I don't understand what CopyBits does. CopyBits has no information about windows, and palettes are only connected to windows, not pixmaps or devices, so I don't see how copybits can use a palette, since I can't think of any way for it to find the palette. (Well, I can think of ways, but they seem unreasonable: e.g. finding the frontmost window that overlaps the global coordinates of the pixmap. Or perhaps CopyBits recovers the destination window by comparing its destination pixmap with every possible window's pixmap.)
  180.  
  181. Can you clarify this for me? Bit 14 of ctFlags still seems very relevant to solving my problems, but I can't seem to get consistent behavior out of it, probably because I don't understand what it's supposed to do. I can't imagine how palettes affect copybits, since by rights copybits has no way of knowing which palettes would be relevant.
  182.  
  183. Thanks again for the help on this. I feel close to understanding something that's been mysterious for a long time.
  184.  
  185. regards
  186.  
  187. denis pelli
  188. -----------------------------------------
  189. Hi Denis,
  190.  
  191. Thanks for the article. We (I) can get so involved in Apple technology that it
  192. becomes easy to lose sight of what's going on outside the company. I guess
  193. that's a variation of Apple's NIH (not invented here) syndrome. My mailing
  194. address is:
  195.  
  196. Don Moccia
  197. Apple Computer, Inc.
  198. MS 303-2T
  199. 1 Infinite Loop
  200. Cupertino, CA 95014
  201.  
  202. Now to ctFlags' bit 14.
  203.  
  204. Restrict yourself to thinking of it in the case of a CopyBits from an offscreen
  205. graphics environment, be it PixMap or GWorld, to a window. This is the only
  206. case where it comes into play. As we've said, when you set bit 14 in the
  207. offscreen's color table, you are telling the system to consider the value field
  208. of each color table entry as an index into the destination color table. The
  209. actual RGB values are ignored. For example, if a pixel in the source (indexed
  210. color) PixMap has a value of 9, and the ninth entry in the source color table
  211. contains 6 in its value field, that pixel will be mapped to the sixth entry in
  212. the palette attached to the destination window. (I'm assuming srcCopy.) I think
  213. you probably already understood this. By the way, you can try unsetting bit 14
  214. in the "Out of This GWorld" snippet I mention previously to see its effect.
  215.  
  216. So how is the connection made? QuickDraw knows it is drawing to a window by
  217. looking at the baseAddr field of the PixMap. A window's baseAddr is always the
  218. same as the main screen's PixMap. A color window has a CGrafPort. A CGrafPort
  219. has the grafVars field which contains the handle to an attached palette (if
  220. any). This last fact is documented somewhat obscurely in Inside Mac V, e.g.  p.
  221. V-113. I hope we'll describe this connection more clearly in the new Palette
  222. Manager chapter. Another article that might help fill in the pieces is
  223. "Multiple Screens Revealed" in develop Issue #10. It doesn't talk about bit 14,
  224. but it gives some insight into how QuickDraw handles windows and offscreens.
  225.  
  226. So this is how the connection is made. Remember, QuickDraw was developed over
  227. ten years ago, so calls such as CopyBits know more about the System than they
  228. would in a modern object-oriented environment. Things will evolve however.
  229.  
  230. Anyway, let me know if there are still missing pieces.
  231.  
  232. Regards,
  233.  
  234. Don Moccia
  235. Apple Developer Support
  236. Reference Number 117781
  237.  
  238. ---------------------------
  239. Dear Don
  240.  
  241. Click! I think I've got it now. However, I'm curious. You implied that the bit 14 mechanism only works if the destination is a window. I would like to use it when the destination is a GWorld too. Is there some logical reason for CopyBits to prevent this? Why not do it whenever bit 14 of the source is set and grafVars of the destination is not NULL? (The consistent treatment of GWorlds and windows is one of the nicest features of 32-bit quickdraw, seems a shame to break it for no reason.)
  242.  
  243. Thanks again. The reprint will go out tomorrow morning (actually 2, one showing some neat research on human letter recognition that we did using QuickDraw).
  244.  
  245. regards
  246.  
  247. denis
  248.  
  249. Denis Pelli
  250. Syracuse University
  251.  
  252. p.s. Vis a vis documentation. The documentation of NewGWorld and UpdateGWorld are a touch brief. If they are ever revised it would be nice if they spelled out the fact that when the boundsRect is interpreted as being in global coordinates the GWorld's portRect always has its upper left corner set to local coordinate (0,0). I thought I'd found a bug, until I carefully re-read the documentation. (The documentation of boundsRect for UpdateGWorld is copied word for word from NewGWorld, including the references to NewGWorld where it obviously means UpdateGWorld.)
  253.  
  254. ------------
  255. p.p.s. wait. I still don't see how CopyBits knows which window, since it only has the pixmap. Oh, I see it now; and this answers my question about GWorlds. CopyBits must compare the handle of the pixmap with the portPixMap field of EVERY window until it finds the right window. It can't do this for GWorlds because there's no globally available list of GWorlds.
  256.  
  257. Is that right?
  258.  
  259. regards
  260.  
  261. denis
  262.  
  263. ---------------
  264. denis,
  265.  
  266. I agree 100% with your wish to use palettes and bit 14 seamlessly across
  267. GWorlds and windows. I wasn't around when the design decisions were made, so I
  268. don't know if it was a "time to market" issue or if there were major technical
  269. obstacles.
  270.  
  271. One of the big problems for a company, when you are committed to compatibility
  272. and have a large installed base of varying machines, is it is hard to make
  273. changes without breaking someone's app. Even if you make the change
  274. successfully, it requires a large SQA effort to ship the product. I wouldn't be
  275. surprised if it came down to someone thinking, perhaps correctly, that it was
  276. too great a risk given the deadlines and resources available at the time.
  277.  
  278. The major priorities in QuickDraw, not counting GX, are finishing the port to
  279. PowerMac and correcting bugs (either old or created by the port). So I can't
  280. say if the Palette Manager will be broadened in the future or if it will be
  281. replaced by a new paradigm. If I hear anything of interest (that I can talk
  282. about), I'll make sure you hear about it.
  283.  
  284. Again, thanks for the papers. Perhaps, I'll be asking you some questions!
  285.  
  286. Regards,
  287.  
  288. Don Moccia
  289.